<?php

class fValidatorString extends fValidator {
	
	public function __construct($options) {
		$this->addMessage('max_length', '"%value%" is too long (%max_length% characters max)!');
    	$this->addMessage('min_length', '"%value%" is too short (%min_length% characters min)!');
    	$this->setOption('max_length', 0);
    	$this->setOption('min_length', 0);
    	$this->setOption('trim', true);
    	parent::__construct($options);		
	}
	
	/**
	 * Validate
	 *
	 * @param string $value
	 * @return boolean
	 */
	public function validate($value) {
		$validate = true;
		$value = (string) $value;
		if ($this->getOptions('trim')) {
			$value = trim($value);		
		}
		$length = (function_exists('mb_strlen'))? mb_strlen($value) : strlen($value);		
		if (($max = $this->getOptions['max_length'] > 0) && ($length > $max)) {
			$this->_failuresOptions[] = 'max_length';
			$this->addMessage('min_length', 
								strtr($this->getMessage('min_length'), 
										array('%value%' => $value, '%max_length%' => $max)));
			$validate = false;
		}
		
		if (($min = $this->getOptions('min_length') > 0) && ($length < $min)) {
			$this->_failuresOptions[] = 'min_length';
			$this->addMessage('min_length', 
								strtr($this->getMessage('min_length'), 
										array('%value%' => $value, '%min_length%' => $min)));
			$validate = false;
		}
		
		return $validate;
	}	
}